/*
 * @lc app=leetcode.cn id=821 lang=javascript
 *
 * [821] 字符的最短距离
 */

const { request } = require("http");

// @lc code=start
/**
 * @param {string} s
 * @param {character} c
 * @return {number[]}
 */
var shortestToChar = function (s, c) {
  let answer = Array(s.length);
  s.split('').forEach((item, index) => {
    if (item === c) {
      answer[index] = 0;
      for (let i = 0; ; i++) {
        if (index - i < 0) {
          break;
        }
        if (answer[index - i] === undefined) {
          answer[index - i] = i;
          continue;
        }
        if (answer[index - i] < i) {
          break;
        }
        answer[index - i] = i;
      }
      for (let i = 0; ; i++) {
        if (index + i >= s.length) {
          break;
        }
        if (answer[index + i] === undefined) {
          answer[index + i] = i;
          continue;
        }
        if (answer[index + i] < i) {
          break;
        }
        answer[index + i] = i;
      }
    }
  })
  return answer;
};
// @lc code=end

